home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1379 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.2 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Subject: Re: size of vtable
  3. From: ufo@sbbs.se (Urban Fosseus)
  4. X-Newsreader: WinVN 0.99.7
  5. References: <4cr1ob$1cu@seal.abalon.se>
  6. MIME-Version: 1.0
  7. Content-Type: Text/Plain; charset=US-ASCII
  8. NNTP-Posting-Host: ppp22.sbbs.se
  9. Message-ID: <30f2dee6.0@news.sbbs.se>
  10. Date: 9 Jan 96 21:32:22 GMT
  11. Path: news.sbbs.se!
  12.  
  13. In article <4cr1ob$1cu@seal.abalon.se>, ken1@abalon.se says...
  14. >
  15. >Is it possible to know the size of the vtbl for a given class 
  16. >at compile time (to do something like
  17. >
  18. >int x = sizeof(theclass::vtbl)
  19. >
  20. >or similar)?
  21. >
  22.  
  23. If you have control over the base class, you can
  24. evaluate the offset of the first data member with code like
  25.  
  26. int sz_vtab = ((int)(&((myclass *)1)->data)-1);
  27.  
  28. Assuming all vtables has the same size, you could also evaluate 
  29. the size of a class that is nothing but a virtual function
  30.  
  31. class MyHiddenLocalClass {
  32.     virtual ~MyHiddenLocalClass();
  33. };
  34.  
  35. int sz_vtab = sizeof(MyHiddenLocalClass);
  36.  
  37. Both approaches might fail if the compiler aligns data members and
  38. rounds struct/class sizes in unpredicted ways. Strictly speaking,
  39. the vtable itself is not required by the standard, but its effects.
  40. So this is not strictly portable code, but code that you can prove
  41. works for all interesting platforms ;-)
  42.  
  43. // Urban
  44.  
  45.